<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cate;
use App\Models\Position;
use Illuminate\Support\Facades\DB;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $positions = Position::get();
        return view('admin.cate.index',compact('positions'));
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $model = new Cate();
        $res = $request->only(['id','name','status','level']);
        if (!empty($res)){
            if ($res['id']) $model = $model->where('id','like','%'.$res['id'].'%');
            if ($res['name'])  $model = $model->where('cate_name','like','%'.$res['name'].'%');
            if ($res['status']!=3 && isset($res['status'])) $model = $model->where('status',$res['status']);
            if ($res['level'] && isset($res['level'])) $model = $model->where('level',$res['level']);
        }
        $res = $model->orderBy('sort','desc')->orderBy('id','desc')->paginate($request->get('limit',30))->toArray();

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    //添加商品页面
    public function create()
    {
        return view('admin.good.create');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cate = Cate::findOrFail($id);
        return view('admin.cate.edit',compact('cate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $cate = Cate::findOrFail($id);
        $data = $request->except('password');
        if ($cate->update($data)){
            return redirect()->to(route('admin.cate'))->with(['status'=>'更新分类成功']);
        }
        return redirect()->to(route('admin.cate'))->withErrors('系统错误');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function upStatus(Request $request)
    {
        $model = new Cate();
        $data = $request->only(['id','status']);
        if ($model->where('id',$data['id'])->update($data)){
            return response()->json(['code'=>0,'msg'=>'修改成功']);
        }
        return response()->json(['code'=>1,'msg'=>'修改失败']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        if (Good::destroy($ids)){
            return response()->json(['code'=>0,'msg'=>'删除成功']);
        }
        return response()->json(['code'=>1,'msg'=>'删除失败']);
    }
}
